Removed extraneous test code and added better exception handling