Added error handling and homogenized code.
Fixing merge issues.