works, but has slight code duplication