fix - some badcoding, but its ok for now. Refactoring strongly recommended