Did some optimization, moved more func to core and added some comments.