reworked:modularized. Probably not completely working