Made middleware API more friendly, added more context support