first working version
added middleware functionality