General code refactoring. Incomplete...