major bug fixes, better tested