Refactror and cleanup