added if statement around some operations to fix API 19 problem