new code to replace old code

rewrite almost everything to fix engine