Error code fix and code clean up