added main logic; still highly buggy
extremely ugly but totally working!