ugly ugly code... but works