Code cleanup
Being code inline with that in online libraries repository.
Ensure all exceptions are handled