Refactoring and removed old code.
Added new implementation.
Fixed some errors.