Cleaned up the (hackish) code slightly