Refactored layout of code. Made standalone check more strict.