complete rework. holding old code for reference. will be deleted soon