some refactoring and bugfixes...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>