add shutdown mechanism to server
need to clean up and optimize