started buildding [hopefully] more robust code based on some papers i found