Modified app file to use middleware efficiently