Restructure code. Minor fixes.