Rewritten server to handle middleware better