Modified and tidied up server side code