fixed middleware and added additional api methods. refactored security middleware