cleaned up codes, removed unused functions, added maor comments