Revert "made no progress"

This reverts commit 077c7bf19a4eb131e02283b1f08a6bbbff66bfbd.
critical bug fix, optimization, code restructure