Messy, but works. Need to put all the checks into module. Bugs are fixed.