Basic refactoring and optimization

Basic refactoring and optimization, please review this update carefully:

* Remove unnecessary code and comments
* Speed up the code
* More code formatting

May be I'm wrong but now the device behaves a little bit more faster :)