Migrated code to dedicated libs
Added middleware with credit to @rmoriz