Borrowing some helper functions from avsm.