Refactoring and tests

The code has been refactored (add new modules and fix some process) and add several tests (run as: rebar eunit skip_deps=true)