refactoring and making less trips to the server.