Refactoring, typos and some optimizations