borrowing some tricks from spf13