merged some fixes and added mask to output