code refactor / bug fixing

code refactor / bug fixing.