Updated code to match that on the server (where small changes has been made)