David, still on the run from Saul, heads to the Philistines to take refuge, and ends up in Gath the hometown of Goliath! This was a very strange thing for David to do as he has also killed thousands of Philistines, as well as Goliath. So David is captured by them and imprisoned. David knowing he has done wrong does the right thing and cries out to God, this is when he wrote Psalm 56 and it can be seen in this Psalm how David's heart is turned back to the Lord as he cries out for mercy. He is then brought before Achish the king, David pretends to be mad which must have taken great humility to act that way before his enemies so Achish releases David and on his way from Gath David praises the Lord mightily, Psalm 34. The Lord showed His love to David even though David did not deserve it, which shows the great love and mercy of God. David then heads for the cave of Adullam from which you can see the valley where David slew Goliath and David lamented and wrote Palm 142.