Added proper error statuses/messages and cleaned up code