rewritten to support multiple instances