Cleaned up exception handling and added some validation checks in encoders