Bux fixing and first refactoring

Signed-off-by: Carlo Caione <carlo@caione.org>