Fix code logic. Fixes #581. Fixes #574.
Merge pull request #582 from s0undt3ch/develop

Fix code logic. Fixes #581. Fixes #574.