Server side code refactoring. Use of promises, and improved error handling