implement error handling and refactor some code
implement app embedding as middleware