implemented a semi-efficient solution