fixed series of bugs that was causing crash and infinite recursion