added key locks, and code refactoring