ported. comments need to be updated, though.