Added error checking + comments, removed junk and fixed some formatting