major refactorings - not workable yet