complete debug, need to improve performance