alternative impl of filterM