Refactor

split into files, added error handling and client