Added error handling and fixed bugs with post insetion