Refactored cortex

@pimnijdam could you QA and merge.. should be merged after the development of aim is merged with the master