Refactor services to library

* Made middleware for requests
* Better handling of failures
* Added request dispatcher