removed tail calls to avoid callstack overflow