Improved comments and error checking