porting to cortexjs
ported more logic to cortex