Refactor

Refactoring the code. May need some tests on the odroid before being ready for production.