Cleanup in codes
Fixing issue with external->internal

This issue occurred because of older code which handled comparing
codes.

Also made sure that there was better error handling.