fixed stack overflow issue, misc bugfixes