overhauled a lot, code is ready for testing