Refactor and Added Tests

Hi,

I've refactored a bit and added tests for the server.
I'm using this for a project and I need to rely on this project :)

Let me know, if there is anything needs to be added to this PR.