More complete and secure version of server script