fix few logic problems