created controller for server and reorganised the code