updated; more debugging and tuning needed