Major code refactoring

Signed-off-by: Vlad <vg@vladgh.com>