Refactor all logic out of sinatra file