Restructured code for consistency, added comments, fixed issue with getConnection()