cleaned up/organized code

Improved efficiency and clarity of error code