new function, some optimization, heavily commented