#78 reorganize and improve code